let saveBth = document.querySelector('.save')

let oldPasswordEl = document.querySelector('.old_pas')
let newPasswordEl = document.querySelector('.new_pas')
let subPasswordEl = document.querySelector('.pas')
// 提示
let oldPwdEl = document.querySelector('.old_Pwd')
let newPwdEl = document.querySelector('.newPwd')
let wordEl = document.querySelector('.password')



let user = localStorage.user && JSON.parse(localStorage.user)
// console.log(user);

//   用户名获取
let mingZi = document.querySelector('.mingzi')
userName()
async function userName() {

    let res = await axios({
        method: 'GET',
        url: '/api/user',
        params: {
            userId: user.userId,
        }
    })
    let html = ''
    html += `
                ${res.data.data.username} 
            `
    mingZi.innerHTML = html
}


saveBth.addEventListener('click', changePassword)
async function changePassword(e) {

    let password = oldPasswordEl.value.trim()
    let newPassword = newPasswordEl.value.trim()
    let pw = subPasswordEl.value.trim()

    let passwordReg = /^(?=.*[a-zA-Z])(?=.*\d)\S{6,12}$/

    // 都为空的时候
    if (!password || !newPassword || !pw) {
        return alert('不能为空')
    }
    // // 新密码和确认密码不填时
    if (!passwordReg.test(newPassword)) {
        return alert('密码格式不对')
    }
    if (newPassword != pw) {
        return alert('两次密码不一致')
    }
    // 发请求，添加数据
    let res = await axios({
        method: 'POST',
        url: '/api/user/revise/pswd',
        data: {
            userId: user.userId,
            password: password,
            newpassword: newPassword

        }
    })
    console.log(res);

    let {
        code,
        msg,
        data
    } = res.data

    // 请求错误
    if (code) {
        return alert(msg)
    }
    // // 存储当前用户的信息 (如果已经登录就不用重新保存 localStorage.user 里的账号)
    // localStorage.user = JSON.stringify(data)
    // // 跳转页面至首页
    location.href = './set_message.html'

}